package com.weme.mine.activity

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.text.TextUtils
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.common.base.BaseViewModel
import com.weme.common.util.UserMKHelper.decodeString
import com.weme.common.util.UserMKHelper.encode
import com.weme.common.util.CommonUtils
import com.weme.common.util.showToast
import com.weme.mine.R
import com.weme.mine.bean.LanguageBean
import com.weme.mine.databinding.ActivityLanguageBinding
import com.weme.common.util.setOnFastClickListener
import com.weme.common.util.setVerticalLayoutManager
import java.util.*

@Route(path = ARouterPaths.MINE_LANGUAGE_ACTIVITY)
class LanguageActivity : BaseMvvmActivity<ActivityLanguageBinding, BaseViewModel>() {
    var languageCode = ""
    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).titleBar(mBinding.customTopNavigationBar).init()
        val settingItems = listOf(
            LanguageBean(getString(com.example.resources.R.string.language_english), "en", false),
            LanguageBean(getString(com.example.resources.R.string.language_bengali), "bn", false),
            LanguageBean(getString(com.example.resources.R.string.language_korean), "ko", false),
            LanguageBean(getString(com.example.resources.R.string.language_spanish), "es", false),
            LanguageBean(getString(com.example.resources.R.string.language_arabic), "ar", false),
            LanguageBean(getString(com.example.resources.R.string.language_french), "fr", false)
        )

        mBinding.rcvList.setVerticalLayoutManager()
        mBinding.rcvList.adapter = languageAdapter
        languageAdapter.addAll(settingItems)
        if(decodeString("language").isNotEmpty()){
            settingItems.forEach {
                if(decodeString("language").equals(it.languageCode)){
                    it.select = true
                    return@forEach
                }
            }
            showToast(decodeString("language"))
        }else{
            val systemLanguage: String? = CommonUtils().getCurrentSystemLanguage()
            settingItems.forEach {
                if(systemLanguage.equals(it.languageCode)){
                    it.select = true
                    return@forEach
                }
            }
            showToast(systemLanguage)
        }

        languageAdapter.setOnItemClickListener { _, _, position ->
            languageAdapter.items.forEachIndexed { index, languageBean ->
                languageBean.select = index == position
            }
            languageAdapter.notifyDataSetChanged()
            mBinding.srvDelSave.alpha = 1F
            languageCode = settingItems.get(position).languageCode

        }
        mBinding.srvDelSave.setOnFastClickListener{
            if(TextUtils.isEmpty(languageCode)){
                return@setOnFastClickListener
            }
            encode("language",languageCode)
            val spanishLocale = Locale(languageCode)
            Locale.setDefault(spanishLocale)

            val config = Configuration()
            config.setLocale(spanishLocale)
            createConfigurationContext(config)
            recreate()
        }

    }

    private val languageAdapter = object : BaseQuickAdapter<LanguageBean, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: LanguageBean?) {
            holder.setText(R.id.tvName, item?.languageName)
            holder.setVisible(R.id.imClickble,item!!.select)


        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_language, parent)
        }


    }

    override fun getViewModelClass(): Class<BaseViewModel> = BaseViewModel::class.java

    
}